package com.ylbh.modules.business.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.business.dto.PrefabricatedGoodsDTO;
import com.ylbh.common.pojo.businessgoods.dto.BusinessGoods1DTO;
import com.ylbh.common.pojo.businessgoods.dto.PrefabricatedGoodsDetailsDTO;
import com.ylbh.common.pojo.businessgoods.dto.UpdateBusinessGoodsDTO;
import com.ylbh.common.pojo.businessgoods.vo.BusinessGoodsDetailsVO;
import com.ylbh.common.pojo.businessgoods.vo.PrefabricatedGoodsClassVO;
import com.ylbh.common.pojo.businessgoods.vo.PrefabricatedGoodsDetailsVO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.OSSClientUtil;
import com.ylbh.common.utils.Ref;
import com.ylbh.modules.business.entity.ShoppingBusinessGoods;
import com.ylbh.modules.business.service.ShoppingBusinessGoodsService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ylbh.common.utils.PageUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;


/**
 * 联盟店商品表
 *
 * @author czx
 * @date 2019-04-16 17:08:02
 */
@RestController
@RequestMapping("business/goods")
public class ShoppingBusinessGoodsController {
    @Autowired
    private ShoppingBusinessGoodsService shoppingBusinessGoodsService;

    @ApiOperation("联盟店商品列表")
    @RequestMapping("queryBusinessGoodsList")
    @RequiresPermissions("business:goods:queryBusinessGoodsList")
    public CommonResult<PageUtils> queryBusinessGoodsList(BusinessGoods1DTO businessGoodsListDTO){
        PageUtils goodsList = shoppingBusinessGoodsService.queryBusinessGoodsList(businessGoodsListDTO);
        return Ref.ok(goodsList);
    }

    @ApiOperation("未审核联盟店商品")
    @RequestMapping("queryCount")
    public CommonResult<Integer> queryCount() {
        int count = shoppingBusinessGoodsService.queryCount();
        return Ref.ok(count);
    }

    @SysLog("对联盟店的商品进行上架操作")
    @ApiOperation("对联盟店的商品进行上架操作")
    @RequestMapping("modifyIsSale")
    @RequiresPermissions("business:goods:modifyIsSale")
    public CommonResult<Void> modifyIsSale(@RequestBody Long goodsId){
        shoppingBusinessGoodsService.modifyIsSale(goodsId);
        return Ref.ok();
    }

    @SysLog("对联盟店的商品进行批量审核操作")
    @ApiOperation("对联盟店的商品进行批量审核操作")
    @RequestMapping("modifyBatchIds/{batch}")
    @RequiresPermissions("business:goods:modifyBatchIds")
    public CommonResult<Void> modifyBatchIds(@RequestBody String[] ids, @PathVariable Integer batch){
        shoppingBusinessGoodsService.modifyBatchIds(Arrays.asList(ids), batch);
        return Ref.ok();
    }

    @SysLog("对联盟店的商品进行下架操作")
    @ApiOperation("对联盟店的商品进行下架操作")
    @RequestMapping("modifyNoIsSale")
    @RequiresPermissions("business:goods:modifyNoIsSale")
    public CommonResult<Void> modifyNoIsSale(@RequestBody Long goodsId){
        shoppingBusinessGoodsService.modifyNoIsSale(goodsId);
        return Ref.ok();
    }

    @ApiOperation("根据id查看联盟店的商品及商家信息")
    @RequestMapping("getGoodsById/{goodsId}")
    @RequiresPermissions("business:goods:getGoodsById")
    public CommonResult<BusinessGoodsDetailsVO> getGoodsById(@PathVariable(value = "goodsId")Long goodsId){
        BusinessGoodsDetailsVO byId = shoppingBusinessGoodsService.getGoodsById(goodsId);
        return Ref.ok(byId);
    }

    @ApiOperation("上传联盟店商品图片")
    @RequestMapping("/uploadImg")
    public CommonResult<String> uploadImg(@RequestBody MultipartFile file){
        if(file==null){
            return Ref.error("上传图片时，图片为空");
        }
        OSSClientUtil ossClient = null;
        try{
            ossClient = new OSSClientUtil();
            String imgUrl = ossClient.uploadCompressImg2Oss(file,"goods");//上传文件到服务器
            if("fail".equals(imgUrl)){
                return Ref.error("上传失败");
            }
            return Ref.ok(imgUrl);
        }catch (Exception e) {									//捕获异常
            e.printStackTrace();
            return Ref.error("上传失败");
        }
    }

    @SysLog("对联盟店的商品详情修改")
    @ApiOperation("对联盟店的商品详情修改")
    @RequestMapping("modifyGoodsById")
    @RequiresPermissions("business:goods:modifyGoodsById")
    public CommonResult<Void> modifyGoodsById(@RequestBody UpdateBusinessGoodsDTO updateBusinessGoodsDTO) {
        shoppingBusinessGoodsService.modifyGoodsById(updateBusinessGoodsDTO);
        return Ref.ok();
    }

    @SysLog("对联盟店的商品审核通过操作")
    @ApiOperation("对联盟店的商品审核通过操作")
    @RequestMapping("modifyAdopt")
    @RequiresPermissions("business:goods:modifyAdopt")
    public CommonResult<Void> modifyAdopt(@RequestBody Long goodsId){
        shoppingBusinessGoodsService.modifyAdopt(goodsId);
        return Ref.ok();
    }

    @SysLog("对联盟店的商品审核不通过操作")
    @ApiOperation("对联盟店的商品审核不通过操作")
    @RequestMapping("modifyNoAdopt/{statusRemarks}")
    @RequiresPermissions("business:goods:modifyNoAdopt")
    public CommonResult<Void> modifyNoAdopt(@RequestBody Long goodsId, @PathVariable String statusRemarks){
        shoppingBusinessGoodsService.modifyNoAdopt(goodsId, statusRemarks);
        return Ref.ok();
    }

    @SysLog("对联盟店的商品批量审核通过操作")
    @ApiOperation("对联盟店的商品批量审核通过操作")
    @RequestMapping("modifyBatchPassByIds")
    @RequiresPermissions("business:goods:modifyBatchPassByIds")
    public CommonResult<Void> modifyBatchPassByIds(@RequestBody String[] ids){
        shoppingBusinessGoodsService.modifyBatchPassByIds(Arrays.asList(ids));
        return Ref.ok();
    }

    @SysLog("对联盟店的商品批量审核不通过操作")
    @ApiOperation("对联盟店的商品批量审核不通过操作")
    @RequestMapping("modifyBatchNoPassByIds")
    @RequiresPermissions("business:goods:modifyBatchNoPassByIds")
    public CommonResult<Void> modifyBatchNoPassByIds(@RequestBody String[] ids){
        shoppingBusinessGoodsService.modifyBatchNoPassByIds(Arrays.asList(ids));
        return Ref.ok();
    }

    @ApiOperation("预制商品列表")
    @RequestMapping("queryPrefabricatedGoods")
    @RequiresPermissions("business:goods:queryPrefabricatedGoods")
    public CommonResult<PageUtils> queryPrefabricatedGoods(PrefabricatedGoodsDTO prefabricatedGoodsDTO){
        PageUtils pageUtils = shoppingBusinessGoodsService.queryPrefabricatedGoods(prefabricatedGoodsDTO);
        return Ref.ok(pageUtils);
    }

    @ApiOperation("预制商品详情")
    @RequestMapping("getPrefabricatedGoodsById/{goodsId}")
    @RequiresPermissions("business:goods:getPrefabricatedGoodsById")
    public CommonResult<PrefabricatedGoodsDetailsVO> getPrefabricatedGoodsById(@PathVariable Long goodsId){
        PrefabricatedGoodsDetailsVO  vo = shoppingBusinessGoodsService.getPrefabricatedGoodsById(goodsId);
        return Ref.ok(vo);
    }

    @SysLog("修改预制商品")
    @ApiOperation("修改预制商品")
    @RequestMapping("modifyPrefabricatedGoodsById")
    @RequiresPermissions("business:goods:modifyPrefabricatedGoodsById")
    public CommonResult<Void> modifyPrefabricatedGoodsById(@RequestBody PrefabricatedGoodsDetailsDTO prefabricatedGoodsDetailsDTO){
        shoppingBusinessGoodsService.modifyPrefabricatedGoodsById(prefabricatedGoodsDetailsDTO);
        return Ref.ok();
    }

    @SysLog("新增预制商品")
    @ApiOperation("新增预制商品")
    @RequestMapping("addPrefabricatedGoods")
    @RequiresPermissions("business:goods:addPrefabricatedGoods")
    public CommonResult<Void> addPrefabricatedGoods(@RequestBody PrefabricatedGoodsDetailsDTO prefabricatedGoodsDetailsDTO){
        shoppingBusinessGoodsService.addPrefabricatedGoods(prefabricatedGoodsDetailsDTO);
        return Ref.ok();
    }

    @SysLog("批量修改预制商品")
    @ApiOperation("批量修改预制商品")
    @RequestMapping("modifyBatchPrefabricatedGoodsById/{batch}/{userClass}")
    @RequiresPermissions("business:goods:modifyBatchPrefabricatedGoodsById")
    public CommonResult<Void> modifyBatchPrefabricatedGoodsById(@PathVariable String batch, @PathVariable Integer userClass, @RequestBody String[] ids){
        shoppingBusinessGoodsService.modifyBatchPrefabricatedGoodsById(batch, userClass, ids);
        return Ref.ok();
    }

    @SysLog("上下架预制商品")
    @ApiOperation("上下架预制商品")
    @RequestMapping("modifyIsSaleByGoodsId/{isSale}")
    @RequiresPermissions("business:goods:modifyIsSaleByGoodsId")
    public CommonResult<Void> modifyIsSaleByGoodsId(@PathVariable String isSale, @RequestBody Long goodsId){
        shoppingBusinessGoodsService.modifyIsSaleByGoodsId(isSale, goodsId);
        return Ref.ok();
    }

    @ApiOperation("预制商品排序")
    @RequestMapping("modifyGoodsOrderNum/{goodsId}/{orderNum}")
    public CommonResult<Void> modifyGoodsOrderNum(@PathVariable String goodsId, @PathVariable String orderNum){
        ShoppingBusinessGoods goods = new ShoppingBusinessGoods();
        goods.setGoodsid(goodsId);
        goods.setOrdernum(Integer.parseInt(orderNum));
        shoppingBusinessGoodsService.updateById(goods);
        return Ref.ok();
    }

    @ApiOperation("预制商品分类")
    @RequestMapping("getPrefabricatedGoodsClass")
    public CommonResult<List<PrefabricatedGoodsClassVO>> getPrefabricatedGoodsClass() {
        List<PrefabricatedGoodsClassVO> list = shoppingBusinessGoodsService.getPrefabricatedGoodsClass();
        return Ref.ok(list);
    }

    @SysLog("商品重新审核")
    @ApiOperation("商品重新审核")
    @RequestMapping("modifyGoodsAdopt/{goodsId}/{status}")
    public CommonResult<Void> modifyGoodsAdopt(@PathVariable String goodsId, @PathVariable Integer status, @RequestBody String statusRemarks) {
        shoppingBusinessGoodsService.modifyGoodsAdopt(goodsId, status, statusRemarks);
        return Ref.ok();
    }

}
